﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using FineUI;
using Newtonsoft.Json.Linq;
using System.Data;
using FTchina;
using FTchina.Library;
using FTchina.DAL;

public partial class _Default  : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        

        if (!IsPostBack)
        {

            // 注册客户端脚本，服务器端控件ID和客户端ID的映射关系
            JObject ids = GetClientIDS(btnExpandAll, btnCollapseAll, mainTabStrip);
            Tree treeMenu = InitTreeMenu();
            ids.Add("mainMenu", treeMenu.ClientID);
            ids.Add("menuType", "menu");
            string idsScriptStr = String.Format("window.IDS={0};", ids.ToString(Newtonsoft.Json.Formatting.None));
            PageContext.RegisterStartupScript(idsScriptStr);


            lblname.Text = "欢迎您：" +MasterHelper.ParentItemName+ MasterHelper.UName;

        }

    }

    private JObject GetClientIDS(params ControlBase[] ctrls)
    {
        JObject jo = new JObject();
        foreach (ControlBase ctrl in ctrls)
        {
            jo.Add(ctrl.ID, ctrl.ClientID);
        }

        return jo;
    }
    #region Tree




    #endregion

    private Tree InitTreeMenu()
    {
        Tree treeMenu = new Tree();
        treeMenu.ID = "treeMenu";
        treeMenu.EnableArrows = true;
        treeMenu.ShowBorder = false;
        treeMenu.ShowHeader = false;
        treeMenu.EnableIcons = false;
        treeMenu.AutoScroll = true;
        Region2.Items.Add(treeMenu);



        // 模拟从数据库返回数据表

        DataSet ds = DBHelper.GetDataSet("select * from menulist order by paixu desc");

        ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["ParentID"]);

        foreach (DataRow row in ds.Tables[0].Rows)
        {

            if (row.IsNull("ParentId") && StringHelper.ChkISinArray(MasterHelper.UserGroup, StringHelper.String2ArrayList(row["power"].ToString())))
            {
                FineUI.TreeNode node = new FineUI.TreeNode();
                node.Text = row["context"].ToString();
                node.NavigateUrl = row["link"].ToString();
                node.Expanded = true;
                treeMenu.Nodes.Add(node);
                ResolveSubTree(row, node);
            }
        }

        ResolveTreeNode(treeMenu.Nodes);


        return treeMenu;
    }

    private void ResolveTreeNode(FineUI.TreeNodeCollection nodes)
    {
        foreach (TreeNode node in nodes)
        {
            if (node.Nodes.Count == 0)
            {
                //if (!String.IsNullOrEmpty(node.NavigateUrl))
                //{
                //    node.IconUrl = ;
                //}
            }
            else
            {
                node.NavigateUrl = "";
                ResolveTreeNode(node.Nodes);
            }
        }
    }
    private void ResolveSubTree(DataRow dataRow, FineUI.TreeNode treeNode)
    {
        DataRow[] rows = dataRow.GetChildRows("TreeRelation");
        if (rows.Length > 0)
        {
            treeNode.Expanded = true;
            foreach (DataRow row in rows)
            {
                if (StringHelper.ChkISinArray(MasterHelper.UserGroup, StringHelper.String2ArrayList(row["power"].ToString())))
                {
                    FineUI.TreeNode node = new FineUI.TreeNode();
                    node.Text = row["context"].ToString();
                    node.NavigateUrl = row["link"].ToString();
                    treeNode.Nodes.Add(node);

                    ResolveSubTree(row, node);
                }
            }
        }
    }


    protected void btn_EndLogin_Click(object sender, EventArgs e)
    {
        RequestHelper.RemoveAllCookies();
        Alert.ShowInParent("您已安全注销", "提示", "parent.window.location.href='/ui/login.aspx'");
    }

}